﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using System.Threading;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Explorer
{
    [Access("258002")]
    public partial class ServerExplorer : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtDir.Text = Server.MapPath("~/");

                this.GV_List.EmptyDataTemplate = GetTemplate(this);
                this.GV_Directory.EmptyDataTemplate = GetTemplate(this);

                BindList();
            }
        }

        #region 当前的物理路径

        /// <summary>
        ///     当前的物理路径
        /// </summary>
        private string Path
        {
            set
            {
                ViewState["Path"] = value;
                this.txtDir.Text = value;
            }
            get
            {
                if (ViewState["Path"] == null)
                {
                    return Server.MapPath("~/");
                }
                return ViewState["Path"].ToString();
            }
        }

        #endregion

        private void BindList()
        {
            var flist = new List<FileInfo>();
            var dic = new Dictionary<string, string>();

            //针对当前目录建立目录引用对象
            var strDir = Path;

            var dirInfo = new DirectoryInfo(strDir);
            var dirSiteRoot = new DirectoryInfo(Server.MapPath("~/"));


            if (dirInfo.FullName.Length > dirSiteRoot.FullName.Length)
            {
                dic.Add("向上", strDir.Substring(0, strDir.LastIndexOf("\\")));
            }

            foreach (var fsi in dirInfo.GetFileSystemInfos())
            {
                //如果是文件
                if (fsi is FileInfo)
                {
                    flist.Add(fsi as FileInfo);
                }
                else
                {
                    dic.Add(fsi.Name, fsi.FullName);
                }
            }

            this.PageNavigation1.RecordCount = flist.Count;

            this.GV_List.DataSource = flist;
            this.GV_List.DataBind();


            this.GV_Directory.DataSource = dic;
            this.GV_Directory.DataBind();
        }

        protected bool CFile(object FullName)
        {
            var fi = new FileInfo(FullName.ToString());
            switch (fi.Extension.ToLower())
            {
                case ".rar":
                    return true;

                case ".zip":
                    return true;
                case ".7z":
                    return true;
            }

            return false;
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                File.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString());

                MsgBox.Message("删除成功！", this.UpdatePanel1);

                BindList();
            }
            catch (Exception ex)
            {
                MsgBox.Message("删除失败，错误信息：" + ex.Message, this.UpdatePanel1);
            }
        }


        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            this.GV_List.PageIndex = e.NewPageIndex - 1;
            BindList(); //要重新绑定数据
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var oldfi = new FileInfo(this.txtOldFilePath.Text);
            var newfi = new FileInfo(this.txtFilePath.Text);

            if (oldfi.Extension != newfi.Extension)
            {
                MsgBox.Message("禁止修改文件后缀名", this.UpdatePanel1);
                return;
            }

            Directory.Move(this.txtOldFilePath.Text, this.txtFilePath.Text.Trim());

            this.Panel1.Visible = false;
            MsgBox.Message("操作成功！", this.UpdatePanel1);
            BindList();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.GV_List.Visible = true;
            this.Panel1.Visible = false;
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strPath = null;


            if (e.CommandName == "Move")
            {
                var index = Convert.ToInt32(e.CommandArgument);
                strPath = this.GV_List.DataKeys[index].Value.ToString();
                this.Panel1.Visible = true;
                this.txtOldFilePath.Text = strPath;
                this.txtFilePath.Text = strPath;
            }
            if (e.CommandName == "CFile")
            {
                if (Winrar.Exists())
                {
                    //Thread th = new Thread(new ParameterizedThreadStart(JieYa));
                    //th.IsBackground = true;
                    //th.Start(e.CommandArgument);

                    var err = JieYa(e.CommandArgument);
                    if (string.IsNullOrEmpty(err))
                    {
                        MsgBox.Message("解压已执行完成.", this.UpdatePanel1);
                    }
                    else
                    {
                        MsgBox.Message(err, this.UpdatePanel1);
                    }
                }
            }
        }

        private string JieYa(object FullName)
        {
            var winrar = new Winrar();
            var fi = new FileInfo(FullName.ToString());
            return winrar.UnCompress(fi.DirectoryName, fi.DirectoryName, fi.Name);
        }

        protected void GV_Directory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strPath = null;
            if (e.CommandName == "GoTo")
            {
                strPath = e.CommandArgument.ToString();

                //MsgBox.Alert(strPath, UpdatePanel1);
                Path = strPath;
                this.PageNavigation1.PageNo = 1;
                this.GV_List.PageIndex = 0;
                BindList();
            }
        }

        protected void btnGoTo_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(this.txtDir.Text.Trim()))
            {
                Path = this.txtDir.Text.Trim();
                this.PageNavigation1.PageNo = 1;
                this.GV_List.PageIndex = 0;
                BindList();
            }
            else
            {
                MsgBox.Message("目录不存在！", this.UpdatePanel1);
            }
        }

        protected void GV_List_Sorting(object sender, GridViewSortEventArgs e)
        {
        }
    }
}